package com.sens.utils;

import android.graphics.Color;
import android.text.SpannableStringBuilder;

import com.sens.alipay.Configure;
import com.sens.alipay.utils.SupportVersion;
import com.sens.common.StringUtil;
import com.sens.common.TimeUtil;
import com.sens.database.GlobalSharedPreferences;
import com.sens.enums.PreferencesSetting;
import com.sens.ipc.alipay.application.Callback;
import com.sens.orderback.OrderBackApplication;
import com.sens.vo.AliPayOrderVo;

public class AlipayLogFilter implements Callback {
    private SpannableStringBuilder ssb = new SpannableStringBuilder("日志:");

    AlipayLogFilter() {
        if (ssb.length() == 3) {
            String log = GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).getString(PreferencesSetting.ALIPAY_HOOK_LOG);
            if (log != null) {
                String lines[] = log.split("\n");
                for (String line : lines) {
                    if (line.length() == 0) continue;
                    ssb.append("\n");
                    switch (line) {
                        case "支付宝已安装":
                        case "支付宝已运行":
                        case "收到测试订单:":
                        case "服务器地址已设置":
                        case "服务器已连接":
                            ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            break;
                        case "还未设置采集方式":
                        case "支付宝未安装":
                        case "支付宝未运行":
                        case "未检测到支付宝用户ID，请主动登录支付宝":
                        case "模拟数据失败":
                        case "服务器地址未设置":
                        case "服务器连接失败":
                            ssb.append(StringUtil.createColorText(Color.RED, line));
                            break;
                        case "可以正常使用了！":
                            ssb.append(StringUtil.createBiggerColorText(Color.BLUE, line));
                            break;
                        default: {
                            if (line.contains("采集方式:")) {
                                ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            } else if (line.contains("支付宝版本:")) {
                                String versionName = line.substring(6);
                                if (SupportVersion.checkSupportable(versionName))
                                    ssb.append(StringUtil.createColorText(Color.BLUE, line));
                                else
                                    ssb.append(StringUtil.createColorText(Color.RED, line));
                            } else if (line.contains("暂不支持当前支付宝版本,支持列表")) {
                                ssb.append(StringUtil.createColorText(Color.RED, line));
                            } else if (line.contains("支付宝用户ID:")) {
                                ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            } else if (line.contains(Configure.monitorNames[0])) {
                                ssb.append(StringUtil.createColorText(Configure.monitorColors[0], Configure.monitorNames[0]));
                                ssb.append(line.substring(Configure.monitorNames[0].length()));
                            } else if (line.contains(Configure.monitorNames[1])) {
                                ssb.append(StringUtil.createColorText(Configure.monitorColors[1], Configure.monitorNames[1]));
                                ssb.append(line.substring(Configure.monitorNames[1].length()));
                            } else if (line.contains(Configure.monitorNames[2])) {
                                ssb.append(StringUtil.createColorText(Configure.monitorColors[2], Configure.monitorNames[2]));
                                ssb.append(line.substring(Configure.monitorNames[2].length()));
                            } else {
                                ssb.append(line);
                            }
                        }
                    }
                }
            }
        }
        noticeLog(ssb);
    }

    void saveLog() {
        GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).putString(PreferencesSetting.ALIPAY_HOOK_LOG, ssb.toString().substring(3));
    }

    void clearLog() {
        GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).putString(PreferencesSetting.ALIPAY_HOOK_LOG, (String) PreferencesSetting.ALIPAY_HOOK_LOG.getDefaultValue());
        ssb.clear();
        ssb.append("日志:");
        noticeLog(ssb);
    }

    @Override
    public void noticeMessage(CharSequence message) {
        if (message == null) return;
        ssb.append("\n");
        ssb.append(message);
        noticeLog(ssb);
    }

    @Override
    public void updateUserId(String userId) {
        if (userId == null) return;
        ssb.append("\n");
        ssb.append(StringUtil.createColorText(Color.BLUE, "支付宝用户ID:" + userId));
        noticeLog(ssb);
    }

    @Override
    public void requestMonitor() {

    }

    @Override
    public void addOrder(AliPayOrderVo aliPayOrderVo) {
        if (aliPayOrderVo == null) return;
        if (OrderBackApplication.getInstance().isAlipayCollectionAble() || "1000100101".equals(aliPayOrderVo.getUserId())) {
            ssb.append("\n");
            if ("1000100101".equals(aliPayOrderVo.getUserId())) {
                ssb.append(StringUtil.createColorText(Color.BLUE, "收到测试订单:"));
                ssb.append("\n");
            }
            for (int i = 0; i < Configure.monitors.length; i++) {
                if (Configure.monitors[i].equals(aliPayOrderVo.getMarkType())) {
                    ssb.append(StringUtil.createColorText(Configure.monitorColors[i], Configure.monitorNames[i]));
                    ssb.append(":" + aliPayOrderVo.getTime());
                    ssb.append("\n");
                    break;
                }
            }
            ssb.append("订单信息:" + aliPayOrderVo);
            ssb.append("\n---------------" + TimeUtil.getFormatTime("yyyy-MM-dd HH:mm:ss") + "---------------");
            noticeLog(ssb);
        }
    }

    public void noticeLog(CharSequence message) {
    }
}
